package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.SoulApi;
import com.tanhua.dubbo.server.pojo.Report;
import com.tanhua.server.mapper.OptionMapper;
import com.tanhua.server.pojo.Answers;
import com.tanhua.server.pojo.Option;
import com.tanhua.server.pojo.User;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created by ${smh} on 2020/12/13.
 */
@Service
public class SoulService {


    @Reference(version = "1.0.0")
    private SoulApi soulApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private QuestionnaireService questionnaireService;

    @Autowired
    private OptionMapper optionMapper;
    /**
     * 问卷列表
     * @return
     */
    public List<ResultVo> paperList() {
        User user = UserThreadLocal.get();
        List<ResultVo> resultVos = null;
        //dubbo远程调用查询是否有最新的报告。
        Report report = this.soulApi.queryReportById(user.getId());
        String reportId = null;
        if(report != null){
           reportId = report.getId().toHexString();
        }

        //把用户的答题等级存到redis中
        String rangeKey = "QUESTION_RANGE"+user.getId();
        //不包含key，解锁初级题
        if(!this.redisTemplate.hasKey(rangeKey)){

            resultVos = this.questionnaireService.queryByType(0,reportId);
            this.redisTemplate.opsForValue().set(rangeKey,"1");
        }else if("1".equals(this.redisTemplate.opsForValue().get(rangeKey))){

            resultVos = this.questionnaireService.queryByType(1,reportId);
            //解锁中级题
            this.redisTemplate.opsForValue().increment(rangeKey);
        }else if("2".equals(this.redisTemplate.opsForValue().get(rangeKey))){

            //解锁高级题
            resultVos = this.questionnaireService.queryByType(2,reportId);
            this.redisTemplate.opsForValue().increment(rangeKey);
        }else if("3".equals(this.redisTemplate.opsForValue().get(rangeKey))){

            resultVos = this.questionnaireService.queryByType(0,reportId);
            this.redisTemplate.opsForValue().set(rangeKey,"1");
        }

        return resultVos;
    }


    /**
     * 存储result
     * @param answers
     * @return
     */
    public String saveReport(List<Answers> answers) {
        User user = UserThreadLocal.get();

        List<String> ids = new ArrayList<>();
        for (Answers answer : answers) {
            //获取到所有的选项id
            ids.add(answer.getOptionId());
        }

        int score = 0;
        for (String id : ids) {
            //根据id查询分数进行累加，得到试卷总分
            Option option = this.optionMapper.queryOneOption(id);
            score += option.getScore();
        }
        //保存试卷
        return this.soulApi.saveReport(score,user.getId());
    }

    /**
     * 根据id查report
     * @param id
     * @return
     */
    public Report queryReport(String id) {
        Report report = this.soulApi.selectReport(id);

        return report;
    }
}
